from domain.domain import Subject
from service.Message import Message,CodeMessageData
import re

class SubjectDAO():
    def __init__(self):
        print("SubjectDAO init")

    def add(self, session, oneSubject):
        """
        :param session: session
        :param oneSubject: one Subject data
        :return: Message Object
        """
        data = []
        try:
            session.add(oneSubject)
            code = 0000
            session.commit()
        except Exception as ex:
            exStr = str(ex)
            exArr = re.search("\([0-9]{4}.*\)", exStr)
            print(exArr[0])
            exTwo = exArr[0].replace("(", "").split(',')[0]
            code = int(exTwo)
            session.rollback()
        if (code not in CodeMessageData):
            code = 2001
        return Message(code, CodeMessageData[code])

    def addAll(self, session, subjectList):
        """
        :param session: session
        :param onePersonalTeam: one personalTeam data
        :return: Message Object
        """
        try:
            session.add_all(subjectList)
            code = 0000
            session.commit()
        except Exception as ex:
            exStr = str(ex)
            exArr = re.search("\([0-9]{4}.*\)", exStr)
            exTwo = exArr[0].replace("(", "").split(',')[0]
            code = int(exTwo)
            session.rollback()
        if (code not in CodeMessageData):
            code = 2001
        return Message(code, CodeMessageData[code])



    def deleteById(self, session, id):
        """
        :param session: session
        :param id: personalTeamid
        :return: result Message Object
        """
        data = []
        try:
            session.query(Subject).filter(Subject.id == id).delete()
            code = 0000
            session.commit()
        except Exception as ex:
            exStr = str(ex)
            exArr = re.search("\([0-9]{4}.*\)", exStr)
            print(exArr[0])
            exTwo = exArr[0].replace("(", "").split(',')[0]
            code = int(exTwo)
            session.rollback()
        if (code not in CodeMessageData):
            code = 2001
        return Message(code, CodeMessageData[code])

    def deleteByJobNumberAndIdlist(self, session, jobNumber, idList, adminFlag=0):
        """
              :param session: session
              :param id: personalTeamid
              :return: result Message Object
              """
        try:
            if (adminFlag):
                for each in idList:
                    session.query(Subject).filter(Subject.id==each).delete()
            else:
                for each in idList:
                    session.query(Subject).filter(Subject.id==each).filter(Subject.job_number==jobNumber).delete()
            code = 0000
            session.commit()
        except Exception as ex:
            exStr = str(ex)
            exArr = re.search("\([0-9]{4}.*\)", exStr)
            print(exArr[0])
            exTwo = exArr[0].replace("(", "").split(',')[0]
            code = int(exTwo)
            session.rollback()
        if (code not in CodeMessageData):
            code = 2001
        return Message(code, CodeMessageData[code])



    def getAllInfo(self, session,jobNumber,DHFlag=0):
        """
        get all info
        :param session: sql session
        :return:
        """
        allData = []
        try:
            if (DHFlag == 1):
                list = session.query(Subject).all()
            else:
                list = session.query(Subject).filter(Subject.job_number==jobNumber).all()
            code = 0000
            for each in list:
                allData.append(each.toDict())
        except:
            code = 1012
        return Message(code, CodeMessageData[code], data=allData)

    def updateByJobNumberAndDataList(self, session, jobNumber, newData,adminFlag=0):
        code = 0000
        try:
            for one in newData:
                if (adminFlag):
                    part = session.query(Subject).filter(Subject.id == one.id).one()
                else:
                    part = session.query(Subject).filter(Subject.id == one.id).filter(Subject.job_number==jobNumber).one()
                part.id = one.id
                part.advisor = one.advisor
                part.award_level = one.award_level
                part.certificate_copy = one.certificate_copy
                part.competition_name = one.competition_name
                part.job_number = one.job_number
                part.number = one.number
                part.remark = one.remark
                part.teams = one.teams
            session.commit()
        except Exception as ex:
            code = 1021
            session.rollback()
        return Message(code, CodeMessageData[code])



    def getDataByJobNumberAndIdList(self, session, jobNumber, idList,adminFlag=0):
        code = 0000
        data=[]
        try:
            for one in idList:
                if (adminFlag):
                    part = session.query(Subject).filter(Subject.id == one).one()
                else:
                    part = session.query(Subject).filter(Subject.id == one).filter(Subject.job_number==jobNumber).one()
                data.append(part.toDict())
        except Exception as ex:
            code = 1021
            data=[]
        return Message(code, CodeMessageData[code],data)

    def getName(self, session, id):
        code = 0000
        data = []
        try:
            result = session.query(Subject).filter(Subject.id == id).one()
            data.append(result.competition_name)
        except:
            code = 1046
            data = []
        return Message(code, CodeMessageData[code], data=data)